package com.zjw.zy.base.special;


import lombok.NonNull;

/**
 * @author ：zhong.jw
 * @date ：Created in 2023/2/1 8:52
 */
public final class Lazy<T> {

    private Object value;

    private final static Object initValue = new Object();

    private final InitValue<T> apply;

    public Lazy(@NonNull InitValue<T> apply) {
        this.apply = apply;
        this.value = initValue;
    }


    @SuppressWarnings("unchecked")
    public T get() {
        if (value != initValue) {
            return (T) value;
        }
        synchronized (this) {
            if (value != initValue) {
                return (T) value;
            }
            return (T) (value = apply.apply());
        }
    }


    public interface InitValue<T> {
        T apply();
    }

    public static void main(String[] args) {
        System.out.println(new Lazy<>(() -> 111111111).get());
    }

}
